package com.yonyou.ftp;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class FtpUtil
{
  public static boolean uploadFile(String url, int port, String username, String password, String path, String filename, InputStream input)
  {
    boolean success = false;

    FTPClient ftp = new FTPClient();
    try
    {
      ftp.connect(url, port);

      ftp.login(username, password);

      int reply = ftp.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return false;
      }

      ftp.setFileType(2);

      if (!ftp.changeWorkingDirectory(path))
      {
        ftp.makeDirectory(path);
      }

      success = ftp.changeWorkingDirectory(path);

      if (success) {
        success = ftp.storeFile(new String(filename.getBytes("GBK"), "iso-8859-1"), input);

        input.close();
      }

      ftp.logout();
    } catch (IOException e) {
      e.printStackTrace();

      if (ftp.isConnected())
        try {
          ftp.disconnect();
        }
        catch (IOException localIOException2)
        {
        }
    }
    finally
    {
      if (ftp.isConnected())
        try {
          ftp.disconnect();
        }
        catch (IOException localIOException3) {
        }
    }
    return success;
  }
}